﻿using DAL_13_0;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Data.TableMapper;

namespace RFID_Interface.DTO
{
    [Serializable]
    public class ICItemInfo
    {
        public String Number { get; set; }
        public String Name { get; set; }
        public String Model { get; set; }
        public String MClass { get; set; }
        public String BaseUnit { get; set; }
        public string PackageUnit { get; set; }
        public Decimal Coefficient { get; set; }
        public String DefaultLoc { get; set; }
        public Decimal Length { get; set; }
        public Decimal Width { get; set; }
        public Decimal Height { get; set; }
        public Decimal Weight { get; set; }
        public String Type1_Number { get; set; }
        public String Type1_Name { get; set; }
        public String Type2_Number { get; set; }
        public string Type2_Name { get; set; }
        public String MProperty { get; set; }
        public Decimal PackageQty_On_Pallet { get; set; }


        public static ICItemInfo Get(string number)
        {
            using (var db = DB.Open())
            {
                var icitem = db.QuerySingle<v_ICItemInfo>(v_ICItemInfo.pNumber == number);
                if (icitem == null) return null;

                var rlt = CreateICItemInfo(icitem);

                return rlt;
            }
        }

        private static ICItemInfo CreateICItemInfo(v_ICItemInfo icitem)
        {
            var rlt = new ICItemInfo();
            rlt.Number = icitem.Number;
            rlt.Name = icitem.Name;
            rlt.Model = icitem.Model;
            rlt.BaseUnit = icitem.BaseUnitName;
            rlt.PackageUnit = icitem.PackageUnitName;
            rlt.MClass = string.Empty;
            rlt.Coefficient = icitem.Coefficient;
            rlt.DefaultLoc = icitem.DefaultLoc;
            rlt.Length = icitem.Length;
            rlt.Width = icitem.Width;
            rlt.Height = icitem.Height;
            rlt.Weight = icitem.Weight;
            rlt.MProperty = icitem.MProperty;
            rlt.Type1_Number = icitem.Type1_Number;
            rlt.Type1_Name = icitem.Type1_Name;
            rlt.Type2_Number = icitem.Type2_Number;
            rlt.Type2_Name = icitem.Type2_Name;
            rlt.PackageQty_On_Pallet = icitem.PackageQty_On_Pallet;
            return rlt;
        }

        public static List<ICItemInfo> GetAll()
        {
            using (var db = DB.Open())
            {
                var icitem = db.QueryList<v_ICItemInfo>(v_ICItemInfo.pNumber.Like("01%"), v_ICItemInfo.pNumber.Arc);
                if (icitem == null) return null;

                return icitem.ConvertAll(CreateICItemInfo);
            }
        }
    }
}